package com.gk.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.study.entity.Thing;
import com.gk.study.mapper.ThingMapper;
import com.gk.study.service.ThingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ThingServiceImpl extends ServiceImpl<ThingMapper, Thing> implements ThingService {
    @Autowired
    ThingMapper mapper;

    @Override
    public List<Thing> getThingList(String keyword, String sort, String c, String cc) {
        QueryWrapper<Thing> queryWrapper = new QueryWrapper<>();

        // 只查询上架状态的新闻
        queryWrapper.eq("status", 0);

        // 搜索
        queryWrapper.like(StringUtils.isNotBlank(keyword), "title", keyword);

        // 排序
        if (StringUtils.isNotBlank(sort)) {
            if (sort.equals("recent")) {
                queryWrapper.orderBy(true, false, "create_time");
            } else if (sort.equals("hot") || sort.equals("recommend")) {
                // 最热排序：综合考虑浏览量、点赞数、收藏数
                queryWrapper.orderBy(true, false, "pv")
                           .orderBy(true, false, "like_count")
                           .orderBy(true, false, "collect_count");
            } else if (sort.equals("top")) {
                queryWrapper.orderBy(true, false, "is_top");
                queryWrapper.orderBy(true, false, "create_time");
            }
        } else {
            queryWrapper.orderBy(true, false, "is_top");
            queryWrapper.orderBy(true, false, "create_time");
        }

        // 根据分类筛选
        if (StringUtils.isNotBlank(c) && !c.equals("-1")) {
            queryWrapper.eq(true, "classification_id", c);
        }

        // 根据推荐状态筛选
        if (StringUtils.isNotBlank(cc) && cc.equals("recommend")) {
            queryWrapper.eq("is_recommend", 1);
        }

        List<Thing> things = mapper.selectList(queryWrapper);

        return things;
    }

    @Override
    public void createThing(Thing thing) {
        thing.setCreateTime(LocalDateTime.now());
        thing.setUpdateTime(LocalDateTime.now());
        thing.setPublishTime(LocalDateTime.now());
        if (thing.getStatus() == null) {
            thing.setStatus(0); // 默认上架状态
        }
        thing.setIsRecommend(0); // 默认不推荐
        thing.setIsTop(0); // 默认不置顶
        thing.setPv(0); // 默认浏览量0
        thing.setLikeCount(0); // 默认点赞数0
        thing.setCollectCount(0); // 默认收藏数0
        thing.setCommentCount(0); // 默认评论数0
        thing.setScore(BigDecimal.ZERO); // 默认评分0

        mapper.insert(thing);
    }

    @Override
    public void deleteThing(String id) {
        mapper.deleteById(id);
    }

    @Override
    public void updateThing(Thing thing) {
        thing.setUpdateTime(LocalDateTime.now());
        mapper.updateById(thing);
    }

    @Override
    public Thing getThingById(String id) {
        Thing thing = mapper.selectById(id);
        return thing;
    }

    // 点赞数加1
    @Override
    public void addWishCount(String thingId) {
        Thing thing = mapper.selectById(thingId);
        if (thing != null) {
            thing.setLikeCount(thing.getLikeCount() + 1);
            mapper.updateById(thing);
        }
    }

    // 点赞数减1
    @Override
    public void subtractWishCount(String thingId) {
        Thing thing = mapper.selectById(thingId);
        if (thing != null && thing.getLikeCount() > 0) {
            thing.setLikeCount(thing.getLikeCount() - 1);
            mapper.updateById(thing);
        }
    }

    // 收藏数加1
    @Override
    public void addCollectCount(String thingId) {
        Thing thing = mapper.selectById(thingId);
        if (thing != null) {
            thing.setCollectCount(thing.getCollectCount() + 1);
            mapper.updateById(thing);
        }
    }

    // 收藏数减1
    @Override
    public void subtractCollectCount(String thingId) {
        Thing thing = mapper.selectById(thingId);
        if (thing != null && thing.getCollectCount() > 0) {
            thing.setCollectCount(thing.getCollectCount() - 1);
            mapper.updateById(thing);
        }
    }

    @Override
    public List<Thing> getUserThing(String userId) {
        QueryWrapper<Thing> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("status", 0); // 只查询上架状态的新闻
        queryWrapper.orderBy(true, false, "create_time");
        return mapper.selectList(queryWrapper);
    }

    @Override
    public List<Thing> getThingListByThingIds(List<Long> thingIdList) {
        QueryWrapper<Thing> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", thingIdList);
        queryWrapper.eq("status", 0); // 只查询上架状态的新闻
        return mapper.selectList(queryWrapper);
    }

    @Override
    public List<Thing> getDefaultThingList() {
        QueryWrapper<Thing> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0); // 只查询上架状态的新闻
        queryWrapper.orderByDesc("pv");
        queryWrapper.last("LIMIT 10"); // 限制返回10条
        return mapper.selectList(queryWrapper);
    }

    @Override
    public List<Thing> getHotThingList(int limit) {
        QueryWrapper<Thing> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0); // 只查上架
        queryWrapper.orderByDesc("like_count")
                    .orderByDesc("collect_count")
                    .orderByDesc("pv");
        queryWrapper.last("LIMIT " + limit);
        return mapper.selectList(queryWrapper);
    }
}
